class FeaturesController < ApplicationController
	before_filter :find_user
	before_filter :login_required
	before_filter :find_feature, :only=>["edit", "update", "destroy"]
	
	def index
		@features = @user.features
	end
	
	def new
		@feature = @user.features.new(:featureable_type=>params[:featureable_type], :featureable_id=>params[:featureable_id])
	end
	
	def create
		@feature = @user.features.create!(params[:feature])
		redirect
    rescue ActiveRecord::RecordInvalid
    	render :action => 'new'
	end
	
	def update
		@feature.update_attributes!(params[:feature])
		redirect
    rescue ActiveRecord::RecordInvalid
    	render :action => 'edit'
	end
	
	def destroy
		@feature.destroy
		redirect
	end
	
	private
	
	def redirect
		redirect_to user_features_path(:user_id=>@user)
	end
	
	def find_user
		login = params[:user_id].blank? ? request.host.downcase : params[:user_id]
		@user = User.find_by_login(login)
	end
	
	def find_feature
		@feature = @user.features.find(params[:id])
	end
	
	def authorized?
		#logged_in? && 
		logged_in?
	end
end
